import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { IView } from "../../mvc/interfaces/IView";
import { PetTitleModule } from "../../../modules/PetTitleModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { SPetInfo } from "../../../Message/Game/SPetInfo";
import { IPetTitleModule } from "../../../modules/interfaces/IPetTitleModule";
import { GameProxy } from "../mvc/GameProxy";
import { SSeqPetTitle } from "../../../Message/Game/SSeqPetTitle";
type int = number;
//class PetTitleController
    
    export  class PetTitleController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.PetTitleOpen, this.onPetTitleOpenHandler);
            Dispatcher.addEventListener(EventName.PetTitleGet, this.onPetTitleGetHandler);
            Dispatcher.addEventListener(EventName.PetTitleGetSuccess, this.onPetTitleGetSuccessHandler);
            Dispatcher.addEventListener(EventName.PetTitleEnable, this.onEnableTitle);
            Dispatcher.addEventListener(EventName.PetTitleHide, this.onHideTitle);
            Dispatcher.addEventListener(EventName.PetTitleIsShowTitle, this.onTitleIsShowTitle);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._titleModule) 
            {
                this._titleModule = new PetTitleModule();
                this._titleModule.addEventListener(WindowEvent.SHOW, this.onViewShow);
                this._titleModule.addEventListener(WindowEvent.CLOSE, this.onViewHide);
            }
            return this._titleModule;
        }

        /* internal  */onViewShow(arg1: WindowEvent): void
        {
            NetDispatcher.addCmdListener(ServerCommand.PetTitleGetCommand, this.onTilteUpdate);
            NetDispatcher.addCmdListener(ServerCommand.PetTitleTimeOut, this.onTilteUpdate);
            NetDispatcher.addCmdListener(ServerCommand.PetTitleRemove, this.onTilteUpdate);
            NetDispatcher.addCmdListener(ServerCommand.PetTitleUpdate, this.onTilteUpdate);
            return;
        }

        /* internal  */onViewHide(arg1: WindowEvent): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.PetTitleGetCommand, this.onTilteUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.PetTitleTimeOut, this.onTilteUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.PetTitleRemove, this.onTilteUpdate);
            NetDispatcher.removeCmdListener(ServerCommand.PetTitleUpdate, this.onTilteUpdate);
            return;
        }

        /* internal  */onPetTitleOpenHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPetInfo;
            this._petInfo = loc1;
            if (view.isHide) 
            {
                view.show();
                if (loc1) 
                {
                    (view as IPetTitleModule).petInfo = loc1;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetTitleGet, loc1.uid));
                }
            }
            else 
            {
                view.hide();
            }
            return;
        }

        /* internal  */onPetTitleGetHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.petTitle.getPetTitleInfo(loc1);
            return;
        }

        /* internal  */onPetTitleGetSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["ssPetTitle"] as SSeqPetTitle;
            cache.petTitle.updateTitleByUid(loc1, loc2);
            this.onTilteUpdate(null);
            return;
        }

        /* internal  */onEnableTitle(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["titleCode"] as int;
            GameProxy.petTitle.enableTitle(loc1, loc2);
            return;
        }

        /* internal  */onHideTitle(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.petTitle.hideTitle(loc1);
            return;
        }

        /* internal  */onTitleIsShowTitle(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["isShow"] as Boolean;
            GameProxy.petTitle.notShowTitle(loc1, !loc2);
            return;
        }

        /* internal  */onTilteUpdate(arg1: Object): void
        {
            if (this._titleModule) 
            {
                this._titleModule.updateOwnTitleList(cache.petTitle.getOwnTitleInfoArray(this._petInfo.uid));
            }
            return;
        }

        private /* var */_titleModule: IPetTitleModule;

        private /* var */_isViewCreate: Boolean=false;

        private /* var */_petInfo: SPetInfo;
    }
